package com.hdjk.view.network;

import com.hdjk.view.callback.AddFavoriteCallBack;
import com.hdjk.view.callback.CallBackListener;
import com.hdjk.view.callback.DelFavoriteCallBack;
import com.hdjk.view.callback.FavoriteListCallBack;
import com.hdjk.view.callback.IsFavoritedCallBack;
import com.hdjk.view.config.Constant;
import com.lst.projectlib.entity.Msg;
import com.zhy.http.okhttp.OkHttpUtils;

import okhttp3.Call;
import okhttp3.MediaType;

/**
 * Created by sxy on 2017/11/3.
 */

public class FavoriteNetWork {
    public static void getFavoriteList(String token, final CallBackListener listener) {
        try {
            OkHttpUtils.get().url(Constant.URL_GET_FAVORITE_LIST)
                    .addHeader("Authorization","Bearer "+token)
                    .build()
                    .execute(new FavoriteListCallBack() {
                        @Override
                        public void onResponse(Msg msg, int arg1) {
                            if (listener != null) {
                                listener.onCallBackCompleted(msg);
                            }
                        }

                        @Override
                        public void onError(Call arg0, Exception arg1, int arg2) {
                            super.onError(arg0, arg1, arg2);
//                            if (arg1.toString().contains("request failed , reponse's code is : 401")){
//                                Preferences pre = new Preferences(Appconfig.instance);
//                                pre.setToken("");
//                                pre.setUserName("");
//                                pre.setHoldingCode(0);
//                                ToastUtils.showLongToast(Appconfig.instance,"登录超时，请重新登录");
//                                return;
//                            }
                            if (listener != null) {
                                Msg msg = new Msg();
                                msg.setMsg(arg1.toString());
                                listener.onCallBackCompleted(msg);
                            }
                        }
                    });
        } catch (Exception e) {
        }
    }

    /**
     * 添加收藏
     * @param token
     * @param jsonInfo
     * @param listener
     */
    public static void addFavorite(String token,String jsonInfo, final CallBackListener listener) {
        try {
            OkHttpUtils.postString().url(Constant.URL_ADD_FAVORITE)
                    .mediaType(MediaType.parse(Constant.json_format))
                    .addHeader("Authorization","Bearer "+token)
                    .content(jsonInfo)
                    .build()
                    .execute(new AddFavoriteCallBack() {
                        @Override
                        public void onResponse(Msg msg, int arg1) {
                            if (listener != null) {
                                listener.onCallBackCompleted(msg);
                            }
                        }

                        @Override
                        public void onError(Call arg0, Exception arg1, int arg2) {
                            super.onError(arg0, arg1, arg2);
//                            if (arg1.toString().contains("request failed , reponse's code is : 401")){
//                                Preferences pre = new Preferences(Appconfig.instance);
//                                pre.setToken("");
//                                pre.setUserName("");
//                                pre.setHoldingCode(0);
//                                ToastUtils.showLongToast(Appconfig.instance,"登录超时，请重新登录");
//                                return;
//                            }
                            if (listener != null) {
                                Msg msg = new Msg();
                                msg.setMsg(arg1.toString());
                                listener.onCallBackCompleted(msg);
                            }
                        }
                    });
        } catch (Exception e) {
        }
    }

    /**
     * 删除收藏
     * @param token
     * @param id
     * @param listener
     */
    public static void delFavorite(String token,String id, final CallBackListener listener) {
        try {
            OkHttpUtils.post().url(Constant.URL_DEL_FAVORITE+"?fundCode="+id)
                    .addHeader("Authorization","Bearer "+token)
                    .build()
                    .execute(new DelFavoriteCallBack() {
                        @Override
                        public void onResponse(Msg msg, int arg1) {
                            if (listener != null) {
                                listener.onCallBackCompleted(msg);
                            }
                        }

                        @Override
                        public void onError(Call arg0, Exception arg1, int arg2) {
                            super.onError(arg0, arg1, arg2);
//                            if (arg1.toString().contains("request failed , reponse's code is : 401")){
//                                Preferences pre = new Preferences(Appconfig.instance);
//                                pre.setToken("");
//                                pre.setUserName("");
//                                pre.setHoldingCode(0);
//                                ToastUtils.showLongToast(Appconfig.instance,"登录超时，请重新登录");
//                                return;
//                            }
                            if (listener != null) {
                                Msg msg = new Msg();
                                msg.setMsg(arg1.toString());
                                listener.onCallBackCompleted(msg);
                            }
                        }
                    });
        } catch (Exception e) {
        }
    }

    public static void isFavorited(String token,String fundCode, final CallBackListener listener) {
        try {
            OkHttpUtils.get().url(Constant.URL_IS_FAVORITED)
                    .addHeader("Authorization","Bearer "+token)
                    .addParams("fundCode",fundCode)
                    .build()
                    .execute(new IsFavoritedCallBack() {
                        @Override
                        public void onResponse(Msg msg, int arg1) {
                            if (listener != null) {
                                listener.onCallBackCompleted(msg);
                            }
                        }

                        @Override
                        public void onError(Call arg0, Exception arg1, int arg2) {
                            super.onError(arg0, arg1, arg2);
//                            if (arg1.toString().contains("request failed , reponse's code is : 401")){
//                                Preferences pre = new Preferences(Appconfig.instance);
//                                pre.setToken("");
//                                pre.setUserName("");
//                                pre.setHoldingCode(0);
//                                ToastUtils.showLongToast(Appconfig.instance,"登录超时，请重新登录");
//                                return;
//                            }
                            if (listener != null) {
                                Msg msg = new Msg();
                                msg.setMsg(arg1.toString());
                                listener.onCallBackCompleted(msg);
                            }
                        }
                    });
        } catch (Exception e) {
        }
    }
}
